<?php

/**
 * @Author: king
 * @Date:   2019-06-05 21:42:59
 * @Last Modified by:   king
 * @Last Modified time: 2020-08-07 20:46:26
 */

namespace App\Http\Controllers\Api\v3;

use App\Http\Controllers\Api\BaseController;
use App\Models\Api\AstroSettingUser;
use App\Tools\Validates\Astroset;
use Illuminate\Http\Request;


/**
 * 星盘配置相关接口
 * Class Astroset
 * @package app\api\controller\v8
 */
class AstrosetController extends BaseController
{
    /**
     * 获取星盘配置
     * @method GET
     * @return \think\response\Json
     */
    public function index(Request $request)
    {
       if (!$request->isMethod('get')) {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $param = $request->all();
        $validate = new Astroset();
        if (!$validate->scene('index')->check($param)) {
            return show(config('code.error'), $validate->getError());
        }

        $map = ['uid' => $request->uid, 'chart' => $param['chart'], 'tid' => $param['tid']];

        $AstroSettingUserModel = new AstroSettingUser();

        $data = $AstroSettingUserModel->getUserSettingByTid($map);

        return show(config('code.success'), '加载成功', $data);
    }

    /**
     * 更新星盘配置
     * @method POST
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        if (!$request->isMethod('put')) {
            return show(config('code.error'), '未知操作', [], 403);
        }
        $param = $request->all();

        $decodeValue = json_decode($param['chart_value'],true);
        if($param['chart'] === 'firdaria' && !isset($decodeValue['firdaria']))
        {
            $decodeValue['firdaria'] = 'default';
        }

        $param['chart_value'] = json_encode($decodeValue,true);
        $validate = new Astroset();
        if (!$validate->scene('update')->check($param)) {
            return show(config('code.error'), (string)$validate->getError());
        }
        $param['uid'] = $request->uid;

        if (!(new AstroSettingUser())->updateUserSetting($param))
        {
            return show(config('code.error'), '更新配置失败');
        }

        return show(config('code.success'), '更新配置成功');
    }

    /**
     * 还原星盘配置
     * @method DELETE
     * @return \think\response\Json
     */
    public function restore(Request $request)
    {
       if (!$request->isMethod('put')) {
            return show(config('code.error'), '未知操作', [], 403);
        }
        //$param = $request->only(['tid','chart']);
         $param = $request->all();
        \Log::info("异常队列信息: " . PHP_EOL . var_export($param, true) . PHP_EOL);
        $validate = new Astroset();
        if (!$validate->scene('delete')->check($param)) {
            return show(config('code.error'), (string)$validate->getError());
        }

        $param['uid'] = $request->uid;

        if (!(new AstroSettingUser())->restoreUserSetting($param))
        {
            return show(config('code.error'), '还原失败');
        }

        return show(config('code.success'), '还原成功');
    }
}
